using EnvDTE;
using System;
using System.CodeDom;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms;
using Fcdbas.WinUI.Forms;
namespace Fcdbas.WinUI.Design
{
	internal class SalFormTypeSerializer : TypeCodeDomSerializer
	{
		public override object Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
		{
			TypeCodeDomSerializer typeCodeDomSerializer = (TypeCodeDomSerializer)manager.GetSerializer(typeof(System.Windows.Forms.Control), typeof(TypeCodeDomSerializer));
			if (typeCodeDomSerializer == null)
			{
				return base.Deserialize(manager, declaration);
			}
			ProjectItem projectItem = (ProjectItem)manager.GetService(typeof(ProjectItem));
			if (projectItem != null)
			{
				string name = declaration.Name;
				CodeClass codeClass = SalFormTypeSerializer.FindClass(projectItem.FileCodeModel.CodeElements, name);
				if (codeClass != null)
				{
					System.Type type = manager.GetType(codeClass.FullName);
					if (type != null)
					{
						try
						{
							//BaseForm.DesignModeType = type;
							return typeCodeDomSerializer.Deserialize(manager, declaration);
						}
						finally
						{
                           // BaseForm.DesignModeType = null;
						}
					}
				}
			}
			return typeCodeDomSerializer.Deserialize(manager, declaration);
		}
		public override CodeTypeDeclaration Serialize(IDesignerSerializationManager manager, object root, System.Collections.ICollection members)
		{
			TypeCodeDomSerializer typeCodeDomSerializer = (TypeCodeDomSerializer)manager.GetSerializer(typeof(System.Windows.Forms.Control), typeof(TypeCodeDomSerializer));
			if (typeCodeDomSerializer == null)
			{
				return base.Serialize(manager, root, members);
			}
			CodeTypeDeclaration result = typeCodeDomSerializer.Serialize(manager, root, members);
			PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(root)["Localizable"];
			if (propertyDescriptor != null && object.Equals(propertyDescriptor.GetValue(root), true))
			{
				System.Type type = System.Type.GetType("System.ComponentModel.Design.Serialization.ComponentCache, System.Design");
				if (type != null)
				{
					IServiceContainer serviceContainer = (IServiceContainer)manager.GetService(typeof(IServiceContainer));
					if (serviceContainer != null)
					{
						System.IDisposable disposable = serviceContainer.GetService(type) as System.IDisposable;
						if (disposable != null)
						{
							disposable.Dispose();
						}
						serviceContainer.RemoveService(type);
					}
				}
			}
			return result;
		}
		private static CodeClass FindClass(CodeElements elements, string name)
		{
			foreach (CodeElement codeElement in elements)
			{
				if (codeElement.Kind == vsCMElement.vsCMElementNamespace)
				{
					CodeClass result = SalFormTypeSerializer.FindClass(codeElement.Children, name);
					return result;
				}
				if (codeElement.Kind == vsCMElement.vsCMElementClass && codeElement.Name == name)
				{
					CodeClass result = (CodeClass)codeElement;
					return result;
				}
			}
			return null;
		}
	}
}
